package com.ctyon.watchmanager.ui.activity;

import android.content.Intent;
import android.os.Bundle;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.ctyon.common.util.DataUtil;
import com.ctyon.common.util.GsonUtil;
import com.ctyon.common.util.SPUtils;
import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.model.net.entity.Manager;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.ui.BaseActivity;
import com.orhanobut.logger.Logger;
import com.umeng.analytics.MobclickAgent;

import java.util.concurrent.TimeUnit;

import androidx.annotation.Nullable;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.ctyon.common.container.ARouterPath.ROUTER_ACTIVITY_MAIN;
import static com.ctyon.common.util.SPUtils.ACCOUNT_NUMBER;
import static com.ctyon.common.util.SPUtils.ACCOUNT_PASSWORD;
import static com.ctyon.common.util.SPUtils.IS_LOGIN;

/**
 * CreateDate：19-6-25 on 上午9:13
 * Describe:
 * Coder: lee
 */
public class LaunchActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Logger.i("onResume");
        try {
            Observable.timer(1, TimeUnit.SECONDS).subscribe(aLong -> {
                Logger.i("onResume : after 1s");
                if (SPUtils.getInstance().getBoolean(IS_LOGIN)) {
                    String number = SPUtils.getInstance().getString(ACCOUNT_NUMBER);
                    String password = SPUtils.getInstance().getString(ACCOUNT_PASSWORD);
                    if (!DataUtil.isEmptyOr(number, password)) {
                        NetService.getInstance()
                                .login(number, password)
                                .timeout(5, TimeUnit.SECONDS)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new BaseObserver<BaseResponseEntity>() {
                                    @Override
                                    public void onSuccess(BaseResponseEntity responseEntity) {
                                        Observable.create((ObservableOnSubscribe<Manager>) emitter -> {
                                            Logger.d("onSuccess : " + responseEntity.toString());
                                            String data = responseEntity.data.toString();
                                            if (data.contains("=")) {
                                                data = data.replace("=", ":");
                                            }
                                            Manager manager = GsonUtil.getGsonInstance().fromJson(data, Manager.class);
                                            BasicApp.getInstance().setManager(manager);
                                            MobclickAgent.onProfileSignIn(manager.getPhoneNumber());
                                            emitter.onNext(manager);
                                        })
                                                .subscribeOn(Schedulers.newThread())
                                                .observeOn(AndroidSchedulers.mainThread())
                                                .subscribe(manager -> {
                                                    Logger.d("aRouter");
                                                    startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                                                    finish();
                                                });
                                    }

                                    @Override
                                    public void onFailing(BaseResponseEntity response) {
                                        goToLogin();
                                    }

                                    @Override
                                    public void onErrorE(Throwable e) {
                                        goToLogin();
                                    }
                                });
                    } else {
                        goToLogin();
                    }
                } else {
                    goToLogin();
                }
            });
        }catch (Exception e){
            Logger.e("Auto login error : " + e);
            goToLogin();
        }

    }

    private void goToLogin() {
        SPUtils.getInstance().put(IS_LOGIN, false);
        startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
        finish();
    }

}
